//
//  ConfirmMovieViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-4.
//
//

#import "ConfirmMovieViewController.h"
#import "NetworkAlertView.h"
#import <QuartzCore/QuartzCore.h>

@interface ConfirmMovieViewController ()

@end

@implementation ConfirmMovieViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //加载视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//加载视图
- (void)init_view
{
    self.title = @"电影票";
    
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 500);
    [myscrollview setContentSize:newSize];

    //加载图片
    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 280, 310)]autorelease];
    imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imgView.image = [UIImage imageNamed:@"表格8行.png"];
    [myscrollview addSubview:imgView];
    
    //座位
    NSArray *seatsArray =[self.seatIds componentsSeparatedByString:NSLocalizedString(@"|", nil)];
    NSMutableString *seatsStr = [[NSMutableString alloc]init];
    for (int i = 0; i < [seatsArray count]; i++) {
        NSArray *singleSeat = [[seatsArray objectAtIndex:i] componentsSeparatedByString:NSLocalizedString(@":", nil)];
        [seatsStr appendString:[NSString stringWithFormat:@"%@排%@号  ",
                                [singleSeat objectAtIndex:0],
                                [singleSeat objectAtIndex:1]]];
    }
    
    //单价
    NSArray *pricesArray =[self.prices componentsSeparatedByString:NSLocalizedString(@"|", nil)];
    int allprice = 0;
    for (int i = 0; i < [pricesArray count]; i ++) {
        allprice += ([[pricesArray objectAtIndex:i] intValue]/100);
    }
    int singleprice = [[pricesArray objectAtIndex:0] intValue]/100;
    
    //电影信息
    NSMutableArray *textarray = [[NSMutableArray alloc]init];
    [textarray addObject:[NSString stringWithFormat:@"电影：%@", self.filmName]];
    [textarray addObject:[NSString stringWithFormat:@"影院：%@", self.cinemaName]];
    [textarray addObject:[NSString stringWithFormat:@"场次：%@    %@", self.hallName, self.time]];
    [textarray addObject:[NSString stringWithFormat:@"座位：%@", seatsStr]];
    [textarray addObject:[NSString stringWithFormat:@"单价：%d元", singleprice]];
    [textarray addObject:[NSString stringWithFormat:@"联系电话：13823642844"]];
    [textarray addObject:[NSString stringWithFormat:@"支付方式：网银支付"]];
    [textarray addObject:[NSString stringWithFormat:@"                                 合计：%d元", allprice]];
    int i;
    for (i = 0; i < [textarray count]; i ++) {
        UILabel *textlbl = [[[UILabel alloc]initWithFrame:CGRectMake(50, 65+30*i, 280, 30)]autorelease];
        textlbl.text = [textarray objectAtIndex:i];
        textlbl.font = [UIFont systemFontOfSize:15];
        textlbl.backgroundColor = [UIColor clearColor];
        textlbl.adjustsFontSizeToFitWidth = YES;
        [myscrollview addSubview:textlbl];
    }
    [textarray release];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 330, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [myscrollview addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:myscrollview];
}

- (void)confirmMessge
{
    //请求座位详情
    MovieManager *moviemanager = [MovieManager shareManager];
    moviemanager.delegate = self;
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    int allprice = 0;
    NSArray *pricesArray =[self.prices componentsSeparatedByString:NSLocalizedString(@"|", nil)];
    for (int i = 0; i < [pricesArray count]; i ++) {
        allprice += ([[pricesArray objectAtIndex:i] intValue]/100);
    }
    [[NetworkAlertView shareView] showNetView];
    [moviemanager confirmonlineTickets:self.orderId
                            cinemaName:self.cinemaName
                                prices:self.prices
                              showtime:[NSString stringWithFormat:@"%@%@", [self.time substringToIndex:2],
                                        [self.time substringFromIndex:3]]
                              showdate:[formatter stringFromDate:[NSDate date]]
                            foretellId:self.foretellId
                                    rk:self.rk
                                  fees:self.fees
                                 count:self.count
                              allprice:allprice];
}

//确认购买
- (void)confirmOnlineTicket:(BOOL)isSuccess
                         tn:(NSString*)tn
{
    [[NetworkAlertView shareView] delView];
    if (isSuccess) {
        [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
    } else {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"下单失败" message:nil delegate:self cancelButtonTitle:@"下单失败，请重新尝试，谢谢" otherButtonTitles: nil] autorelease];
        [baseAlert show];
    }
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

@end
